<?php

namespace App\Admin\Repositories;

use App\Models\ArticleCategory as Model;
use Dcat\Admin\Repositories\EloquentRepository;

class ArticleCategory extends EloquentRepository
{
    /**
     * Model.
     *
     * @var string
     */
    protected $eloquentClass = Model::class;


    /**
     * 获得分类 select选择器
     * @param int $selectUse
     * @param int $parentId
     * @param int $level
     * @param int $id
     * @return mixed
     */
    public static function getList($parentId = 0, $level = 0, $selectUse = 0, $id = 0, $regionId = 0)
    {
        $res = [];
        $query = \App\Models\ArticleCategory::query();
        $sql_region = [];
        $sql_region[] = 0;
        if ($regionId) {
            $sql_region[] = $regionId;
        }
        $query->whereIn('region_id', $sql_region);
        if ($id) {
            $cartgory = $query->where('id', $id)->get();
        } else {
            $query->where('parent_id', $parentId);
            $cartgory = $query->orderBy('category_sort', 'asc')->get();
        }

        if ($selectUse) {
            $res[0] = '一级分类';
        }

        foreach ($cartgory as $cartgoryVal) {
            $res[$cartgoryVal->id] = $cartgoryVal->category_name;
            if ($level >= 2) {
                // 获得2级分类
                $childCartgory = \App\Models\ArticleCategory::where('parent_id', $cartgoryVal->id)->whereIn('region_id', $sql_region)->get();
                foreach ($childCartgory as $childCartgoryVal) {
                    $res[$childCartgoryVal->id] = '&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;'.$childCartgoryVal->category_name;

                    if ($level == 3) {
                        // 获得3级分类
                        $sChildCartgory = \App\Models\ArticleCategory::where('parent_id', $childCartgoryVal->id)->whereIn('region_id', $sql_region)->get();
                        foreach ($sChildCartgory as $sChildCartgoryVal) {
                            $res[$sChildCartgoryVal->id] = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;'.$sChildCartgoryVal->category_name;
                        }
                    }
                }
            }
        }

        return $res;
    }

    /**
     * 获得该分类下的全部子分类
     * @param int $parentId
     * @return int[]
     */
    public static function getChild($parentId = 0)
    {
        if ($parentId == 0) {
            return [0];
        }
        $resId[] = $parentId;
        // 获得二级分类
        $childCategory = \App\Models\ArticleCategory::where('parent_id', $parentId)->get(['id']);
        foreach ($childCategory as $childCategoryVal) {
            $resId[] = $childCategoryVal->id;

            // 获得三级分类
            $sChildCategorry = \App\Models\ArticleCategory::where('parent_id', $childCategoryVal->id)->get(['id']);
            if ($sChildCategorry) {
                foreach ($sChildCategorry as $sChildCategorryVal) {
                    $resId[] = $sChildCategorryVal->id;
                }
            }
        }

        return $resId;
    }

    /**
     * 返回需要区分地区分类ID
     * @return array
     */
    public static function needRegionIDCategory()
    {
        /**
         * 3党务
         * 6文化
         * 7村务
         * 8政策
         * 14党务公开
         * 15党员教育
         * 20乡贤榜
         * 21非遗文化
         * 22名胜古迹
         * 23通知公告
         * 24重大事项
         * 25其他事务
         * 31本村概况
         */

        $data = [];
        $id = \App\Models\ArticleCategory::where('is_region', 1)->get(['id']);
        if (!$id->isEmpty()) {
            $id = $id->toArray();
            $data = array_column($id, 'id');
        }
        return $data;
//        return [3, 6, 7, 8, 14, 15, 20, 21, 22, 23, 24, 25, 31];
    }
}
